import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        File file = new File("D:\\githomework\\java1228\\student\\zhaofh\\HomeWork\\the_joker.jpg");
        File file1 = new File("D:\\githomework\\java1228\\student\\zhaofh\\HomeWork\\TEST.txt");
        try {
            serverSocket = new ServerSocket(3456);
            socket = serverSocket.accept();
            outputStream = socket.getOutputStream();
            fileInputStream = new FileInputStream(file);
            outputStream.write("我是服务端正在向客户端发送图片！".getBytes());
            outputStream.write("我是客户端，我已经收到文本了，谢谢！".getBytes());
            byte[] p = new byte[10000];
            int length1 = fileInputStream.read(p);
            while (length1!=-1){
                outputStream.write(p,0,length1);
            }

            socket.shutdownOutput();

            inputStream = socket.getInputStream();

            fileOutputStream = new FileOutputStream(file1);
            byte[] b = new byte[10000];
            int length = inputStream.read(b);
            while(length!=-1){
                String str = new String(b,0,length);
                System.out.println(str);
                fileOutputStream.write(b,0,length);
//                break;
            }
            socket.shutdownInput();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
